<template>
	<view class="container padding-top">
		<view class="margin-left-sm margin-right-sm margin-bottom bg-white radius10 padding flex  flex-direction">

			<view style="color: #000;">充值金额</view>
			<view class="flex align-center write">
				<text class="cash-icon">¥</text>
				<input v-model="amount" type="number" placeholder="请输入充值金额">
			</view>
			<view class="money flex justify-between text-999">
				<view class="money">可用余额 ¥{{ parseFloat(userInfo.balance/ 100).toFixed(2)}}</view>

			</view>

		</view>
		<view class="btn" @click="submit">充值</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				balance: '',
				userInfo: {},
				amount: ''

			}
		},
		onShow() {

			this.getUser()
		},
		methods: {

			async getUser() {
				const [err, rs] = await this.$get(this.$api.userDetail)
				if (rs) {
					this.userInfo = rs.data
				}
			},
			submit() {
				const that = this
				if (this.amount) {
					uni.showModal({
						content: '你确定充值吗',
						success: res => {
							if (res.confirm) {
								const data = {
									type: 1,
									userId: Number(uni.getStorageSync('userId')),
									amount: this.amount * 100
								}
								this.$jsonPost(this.$api.recharge, data).then(res => {
									const [err, rs] = res
									if (rs) {
										// #ifdef  MP-WEIXIN
										const data = {
											orderId: rs.data.id,
											payWay: 1,
											appId: this.$config.wxAppId,
											openId: uni.getStorageSync('openid')
										};
										// #endif

										// #ifdef  APP-PLUS
										const data = {
											orderId: rs.data.id,
											payWay: 2,
											appId: this.$config.appId,
											openId: uni.getStorageSync('openid')
										};
										// #endif



										this.$get(this.$api.rechargeWxPay, data).then(res => {
											const [error, result] = res
											console.log(error)
											if (result) {
												const data = result.data;

												// #ifdef  MP-WEIXIN
												uni.requestPayment({
													provider: "wxpay",
													appId: data.appId,
													timeStamp: data.timeStamp,
													nonceStr: data.nonceStr,
													package: data._package,
													signType: data.signType,
													paySign: data.paySign,
													success: function(res) {
														console.log("success:" +
															JSON.stringify(res)
														);
														that.getUser()

													},
													fail: function(err) {
														console.log("fail:" + JSON
															.stringify(err));
													},
												});
												// #endif

												// #ifdef  APP-PLUS
												uni.requestPayment({
													"provider": "wxpay",
													"orderInfo": {
														"appid": data.appId, // 微信开放平台 - 应用 - AppId，注意和微信小程序、公众号 AppId 可能不一致
														"noncestr": data.nonceStr, // 随机字符串
														"package": data._package, // 固定值
														"partnerid": data.partnerid, // 微信支付商户号
														"prepayid": data.prepayid, // 统一下单订单号 
														"timestamp": data.timeStamp, // 时间戳（单位：秒）
														"sign": data.sign // 签名，这里用的 MD5 签名
													},
													success(res) {
														that.getUser()
													},
													fail(e) {}
												})
												// #endif
											}
										})

									}

								})
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				} else {
					this.$util.msg('请输入充值金额')
				}
			},

		}
	}
</script>

<style scoped>
	.cash-icon {
		font-size: 54rpx;
		font-weight: 500;
		color: #000;
	}

	.write {
		padding: 60rpx 0 40rpx;
		border-bottom: 2rpx solid #eee;
	}

	input {
		margin-left: 40rpx;
	}

	.money {
		margin-top: 30rpx;
		font-size: 30rpx;
	}

	.btn {
		margin: 80rpx 60rpx 60rpx;
		background: #ED7200;
		border-radius: 20rpx;
		text-align: center;
		height: 80rpx;
		line-height: 80rpx;
		color: #fff;
	}
</style>
